package com.zhaoxianglong.priciple;

import org.junit.Test;

/**
 * @program: design-learning
 * @description: 单一职责原则
 * @author: zhaoxianglong
 * @create: 2021-03-16 14:05
 **/
public class SingleResponsibilityTest {

    /**
     * 违反了单一职责原则
     */
    @Test
    public void test1(){
        Vehicle vehicle = new Vehicle();
        vehicle.run("摩托车");
        vehicle.run("汽车");
        vehicle.run("飞机");
    }

    /**
     * 遵守单一职责原则
     * 改动大
     */
    @Test
    public void test2(){
        RoadVehicle roadVehicle = new RoadVehicle();
        roadVehicle.run("摩托车");
        roadVehicle.run("汽车");

        AirVehicle airVehicle = new AirVehicle();
        airVehicle.run("飞机");
    }

    /**
     * 方法上遵循单一职责原则
     */
    @Test
    public void test3(){
        Vehicle2 vehicle2 = new Vehicle2();
        vehicle2.run("汽车");
        vehicle2.runAir("飞机");
        vehicle2.runWater("轮船");
    }
}

/**
 * 交通工具类
 */
class Vehicle{
    public void run(String vehicle){
        System.out.println(vehicle + " 在公路上跑!!!");
    }
}

class RoadVehicle{
    public void run(String vehicle){
        System.out.println(vehicle + " 在公路上跑!!!");
    }
}

class AirVehicle{
    public void run(String vehicle){
        System.out.println(vehicle + " 在天上飞!!!");
    }
}

class WaterVehicle{
    public void run(String vehicle){
        System.out.println(vehicle + " 在水中游 !!!");
    }
}

class Vehicle2{

    public void run(String vehicle){
        System.out.println(vehicle + " 在公路上跑!!!");
    }
    public void runAir(String vehicle){
        System.out.println(vehicle + " 在天上飞!!!");
    }
    public void runWater(String vehicle){
        System.out.println(vehicle + " 在水中游!!!");
    }
}